package com.jiji.miaosha.handle;

import com.jiji.miaosha.error.BusinessException;
import com.jiji.miaosha.error.EmBusinessError;
import com.jiji.miaosha.util.ResultVOUtil;
import com.jiji.miaosha.vo.ResultVO;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;


@ControllerAdvice
public class ExceptionHandle {

    //定义ExceptionHandler解决未被controller层吸收的exception
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    protected ResultVO handleException(RuntimeException ex) {
//        if (ex instanceof UserException || ex instanceof ItemException) {
//            UserException userException = (UserException) ex;
//            return ResultVOUtil.error(userException.getCode(), userException.getMessage());
//        } else {
//            return ResultVOUtil.error(UserEnum.UNKNOWN_ERROR.getCode(), UserEnum.UNKNOWN_ERROR.getMsg());
//        }
        if (ex instanceof BusinessException){
            BusinessException businessException= (BusinessException) ex;
            return ResultVOUtil.error(businessException.getErrCode(),businessException.getErrMsg());
        }else {
            return ResultVOUtil.error(EmBusinessError.UNKNOWN_ERROR.getErrCode(),EmBusinessError.UNKNOWN_ERROR.getErrMsg());
        }
    }
}
